<?php
class MobileDetailController extends AppController {
	var $name = 'MobileDetail';
	
	var $uses = array (
			'SysParams',
			'Job',
			'Company',
			'CompanyAccount',
			'JobArea',
			'JobCategory',
			'Member',
			'Order',
			'MailJob',
			'BookMark',
			'MemberResume',
			'MemberEducation',
			'MemberWork',
			'MemberExpWorktype',
			'Mobile'
	);
	
	function detail($job_id,$url=null){
		$this->initmobile();
		if(!is_numeric($job_id)) {
			$this->redirect('/errors/error404');
		}
		$job=$this->Job->find('"Job".deleted is null and "Job".id='.$job_id);

		//lfx add 2013-09-13 start -->update html path
		$job['Job']['job_wap_detail_html']=$this->Common->relative_path_set($job['Job']['job_wap_detail_html']);
		
//临时
		$job['Job']['job_wap_detail_html'] = str_replace('src="/kamome/', 'src="http://www.kamome.cn/kamome/', $job['Job']['job_wap_detail_html'] );	
			
		//lfx add 2013-09-13 end -->update html path	 
		//成立年月没有月，删除‘-’
		$last_str = substr($job['Company']['company_created'],-1);
		if($last_str == '-'){
			$job['Company']['company_created'] = substr($job['Company']['company_created'],0,-1);
		}
		$condition = "\"Job\".id=$job_id And \"Job\".job_status_id=1 AND (\"Job\".\"public_start\"||' '||\"Job\".\"public_start_hour\"||':'||\"Job\".\"public_start_min\")<=to_char(now(),'YYYY-MM-DD HH24:MI') AND (\"Job\".\"public_end\"||' '||'23:59:59')>=to_char(now(),'YYYY-MM-DD HH24:MI:SS')" .
		" AND \"Job\".public_close_date is NULL AND \"Job\".deleted is NULL";
		$condition1 = "\"Job\".id=$job_id And \"Job\".job_status_id=1 " .
		" AND (\"Job\".public_close_date is not NULL OR (\"Job\".\"public_start\"||' '||\"Job\".\"public_start_hour\"||':'||\"Job\".\"public_start_min\")>to_char(now(),'YYYY-MM-DD HH24:MI'))" .
		" AND Job.deleted is NULL";
		$public_check = $this->Job->find($condition);
		$public_check1 = $this->Job->find($condition1);
		if(!empty($public_check1) || empty($job)){
			//$this->set('overdate','この求人の募集は終了しています。');
			$this->show4042();
			exit;
		}
		
		if(empty($public_check)){
			$this->set('overdate','この求人の募集は終了しています。');
		}
		if(empty($url)){
			$sys=$this->SysParams->find("\"SysParams\".kbn='area_id' and \"SysParams\".id=".$job['Job']['important_area_id']);
			$this->set('area_img',$sys['SysParams']['description']);
			$this->pageTitle=$sys['SysParams']['title'].'|'.$job['Job']['title'];
			$this->set('area_id',$sys['SysParams']['id']);
			$this->set('area',$sys['SysParams']['description']);
			$this->set('Area_img',$sys['SysParams']['description']);
		}else{
			$sys=$this->SysParams->find("\"SysParams\".description='$url'");
			if($sys['SysParams']['kbn']=='tema_id'){
				$data=$this->SysParams->find("\"SysParams\".kbn='area_id' and \"SysParams\".id=".$job['Job']['important_area_id']);
				$url=$data['SysParams']['description'];
				$this->set('area_id',$data['SysParams']['id']);
				$this->set('area',$data['SysParams']['description']);
				//20081223----content_flg=tema;tema数据;
				$this->set('content_flg','tema');
				$this->set('Area_img',$sys['SysParams']['description']);
			}else{
				$this->set('area_id',$sys['SysParams']['id']);
				$this->set('area',$url);
				//20081223----content_flg=area;area数据;
				$this->set('content_flg','area');
				$this->set('Area_img',$url);
			}
			$this->set('area_img',$url);
		
			$this->pageTitle=$sys['SysParams']['title'].'|'.$job['Job']['title'];
		}
		
		//check bookmark
		$member_id=$this->rdAuth->id;
		if(!empty($member_id)){
			$bookmark=$this->BookMark->find("\"BookMark\".job_id='$job_id' AND \"BookMark\".member_id='$member_id' AND \"BookMark\".deleted is null");
			if(!empty($bookmark)){
				$bookmark_flg=true;
				$this->set('bookmark_flg',$bookmark_flg);
			}
		}
		
		if(!empty($job['Company']['logo_name'])){
			$job['Company']['logo_path']='/'.$this->Prop->getValue('config.image.dir').'/'.floor($job['Company']['id']/1000).'/'.$job['Company']['id'].'/'.$job['Company']['logo_name'];
		}
		
		$otherjobcondition = "\"Job\".company_id=".$job["Company"]["id"]." And \"Job\".job_status_id=1 " .
		" AND (\"Job\".public_close_date is not NULL OR (\"Job\".\"public_start\"||' '||\"Job\".\"public_start_hour\"||':'||\"Job\".\"public_start_min\")>to_char(now(),'YYYY-MM-DD HH24:MI'))" .
		" AND Job.deleted is NULL";

// 		$otherjobs = $this->Job->getJobByCompanyID($job['Company']['id']);

		$job['Job']['post_data']=$this->processSelects($job,'JobPost','post_id');
		$job['Job']['lang_data']=$this->processSelects($job,'JobHopeLanguage','hope_language_id');
		$job['Job']['member_data']=$this->processSelects($job,'JobHopeMember','hope_member_id');
		$job['Job']['attention_data']=$this->processSelects($job,'JobAttention','attention_id');
		if ($job['Job']['company_name_flg'] == '0') {
			$job['Job']['job_company_name'] = $job['Company']['company_name'];
		} elseif($job['Job']['company_name_flg'] == '1') {
			$job['Job']['job_company_name'] = $job['Company']['company_alias'];
		} else{
			$job['Job']['job_company_name'] = $job['Job']['company_name_other'];
		}
		$this->Output->htmlspecialchars_decode($job);
// 		$areaid = isset($job['JobArea'][0])?intval($job['JobArea'][0]['job_area_id']):"9397";
// 		$tameid = isset($job['JobCategory'][0])?intval($job['JobCategory'][0]['category_id']):"";

// 		$this->set('otherjobs',$otherjobs);
		$this->set('job',$job);
		$ref = isset( $_SERVER["HTTP_REFERER"])? $_SERVER["HTTP_REFERER"]:'';
		$reffer = array();
/*		
		if($ref && strpos($ref,'mobile/search'))
		{
			parse_str($ref,$out);
			$areaid = $tameid = '';
			foreach($out as $k=>$v)
			{
				if(strpos($k,'areaid'))
				{
					$areaid = $v;
				}else if($k=='tameid'){
					$tameid = $v;
				}	
			}
			if($areaid)
			{
				$areas=$this->SysParams->generateList("\"SysParams\".kbn = 'area_id' and \"SysParams\".deleted is null and id=".$areaid,"order by id asc");
				$areas[9397] = "中国全土";
				$refname = isset($areas[$areaid])?trim($areas[$areaid]):"";
			}
			if($tameid)
			{
				$temas = $this->Mobile->getMobileCategoryList();
				$refname .= ' & ';
				$refname .= isset($temas[$tameid])?trim($temas[$tameid]):"";
			}
			$refname .= ' の求人';
			$reffer = array('refname'=>$refname,'ref'=>$ref);
		}
*/		
		$this->set('reffer',$reffer);
		$companyaccount=$this->CompanyAccount->find("\"CompanyAccount\".id=".$job['Job']['admin_id']." and \"CompanyAccount\".deleted is null ");
		$this->set("companyaccount",$companyaccount);
		$this->render('jobdetail');
	}
	
	function processSelects($data,$key,$kbn){
		if(!isset($data[$key])){
			return '';
		}
		$arr = array();
		foreach($data[$key] as $d){
			$sysdata=$this->SysParams->find('"SysParams".kbn='.'\''.$kbn.'\''.' and "SysParams".id='.$d[$kbn]);
			if($key=='JobHopeLanguage'&&$d[$kbn]==4){
				$arr[] = $data['Job']['hope_language_note'];
			}else{
				$arr[] = $sysdata['SysParams']['name'];
			}
		}
		return  implode(',',$arr);
		$email = "test";
		$this->Encrypt->encode($email);
	}
}